// packageStore/pages/perfect/business/list.js
import {perfect} from '../../../../js/modules/store';
var app = getApp();
import BasePage from '../../../../js/common/base-page';
import {
  router
} from '../../../../js/router';
BasePage({
  pageKey:"business",
  validateLogin:false,
  /**
   * 页面的初始数据
   */
  data: {
    profileToken:'',
    list:[],
    pageIndex:1,
    pageSize:20,
    hasMoreData: true,
    keyword:"",
    selected:[],    //选中的
    idx:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {title,profileToken,idx} = options;
    let that = this;
    wx.setNavigationBarTitle({
      title: title
    });
    that.setData({
      profileToken:profileToken,
      idx:idx
    },function(){
      that.getList();  //获取数据
    })
    
  },
  /**
   * 获取数据
   */
  getList:function(){
    var that = this;
    var params = {
      Keyword: that.data.keyword,
      PageIndex:that.data.pageIndex,
      PageSize:that.data.pageSize,
      ProfileToken:that.data.profileToken,
    }
    
    perfect.getBusinessItem(params, function (content) {
      var list = that.data.list;
      if (that.data.pageIndex == 1) {
        list = [];
      }
      if (content.count > 0) {
        var contentlist = content.data;
        if (contentlist.length < that.data.pageSize) {
          that.setData({
            list: list.concat(contentlist),
            hasMoreData: false
          });
        } else {
          that.setData({
            list: list.concat(contentlist),
            hasMoreData: true,
            pageIndex: that.data.pageIndex + 1
          });
        }
  
      } else {
        that.setData({
          list: [],
          hasMoreData: true,
          pageIndex: 1
        });
      }
    });
  },
  /**
   * 选择
   */
  checkboxChange:function(e){
    this.setData({
      selected:e.detail.value
    })
  },
  /**
   * 选好了
   */
  sureSelect:function(){
    let {idx,selected} = this.data;
    router.navigateBack({
      businessIdx:idx,
      businessTips:selected.join(',')
    },true);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.pageIndex = 1;
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) { 
      this.getList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})